/**
 * 深度克隆
 */
export function deepClone(target: any, cache = new WeakMap()) {
  if (typeof target !== 'object' || target === null) {
    return target;
  }

  const cached = cache.get(target);

  if (cached) {
    return cached;
  }

  const result: any = Array.isArray(target) ? [] : {};

  Object.setPrototypeOf(result, Object.getPrototypeOf(target));
  cache.set(target, result);

  for (const key in target) {
    if (Object.prototype.hasOwnProperty.call(target, key)) {
      result[key] = deepClone(target[key], cache);
    }
  }

  return result;
}
